package com.ccxe.manage.web.customer;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ccxe.manage.filters.MdPwd;
import com.ccxe.manage.model.CustomerBean;
import com.ccxe.manage.model.RoleBean;
import com.ccxe.manage.model.UserBean;
import com.ccxe.manage.model.UserPasswordBean;
import com.ccxe.manage.model.UserRoleBean;
import com.ccxe.manage.service.CustomerManager;
import com.ccxe.manage.service.UserManager;
import com.ccxe.manage.service.impl.CustomerManagerImpl;
import com.ccxe.manage.service.impl.RoleDaoService;
import com.ccxe.manage.service.impl.UserManageImpl;

public class AddServlet extends HttpServlet {

	private CustomerManager customerManager = new CustomerManagerImpl();

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * 增加操作 通过type值判断进行操作的表
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String key = request.getParameter("key").trim();
		if (key.equals("cus")) {// 添加客户操作
			String customer_id = request.getParameter("customerID");
			String company = request.getParameter("company");
			String contact_name = request.getParameter("contactName");
			String contact_phone = request.getParameter("contactPhone");
			String contact_fax = request.getParameter("contactFax");
			String contact_email = request.getParameter("contactEmail");
			String street = request.getParameter("street");
			String city = request.getParameter("city");
			String country = request.getParameter("country");
			String post_code = request.getParameter("postCode");
			// 赋值
			CustomerBean cb = new CustomerBean();
			cb.setCustomerID(customer_id);
			cb.setCompany(company);
			cb.setContactName(contact_name);
			cb.setContactPhone(contact_phone);
			cb.setContactFax(contact_fax);
			cb.setContactEmail(contact_email);
			cb.setStreet(street);
			cb.setCity(city);
			cb.setCountry(country);
			cb.setPostCode(post_code);
			if (customerManager.isExsitID(customer_id)) {
				int result = customerManager.addCustormer(cb);
				if (result > 0) {
					request.setAttribute("message", "添加客户["+customer_id+"]成功");
					request.getRequestDispatcher(
					"/servlet/SrhCus").forward(
							request, response);
				}else{
					request.setAttribute("message", "添加客户["+customer_id+"]失败");
					request.getRequestDispatcher("/jsp/customer/addUser.jsp")
					.forward(request, response);
				}
			} else {
				request.setAttribute("message", "客户名["+customer_id+"]存在!添加失败");
				request.getRequestDispatcher(
				"/servlet/SrhCus").forward(
						request, response);
			}
		} else if (key.equals("toAddUser")) {
			RoleDaoService roleDaoService = new RoleDaoService();
			List<CustomerBean> customlist = customerManager
					.getByCondition(null);
			List<RoleBean> roleList = roleDaoService.getByCondition();
			if (customlist != null && roleList != null) {
				request.getSession(true).setAttribute("cusList", customlist);
				request.getSession(true).setAttribute("roleList", roleList);
				request.getRequestDispatcher("/jsp/customer/addUser.jsp")
						.forward(request, response);
			}
		} else if (key.equals("user")) {// 添加用户操作
			String userID = request.getParameter("userID");
			UserManager userManager = new UserManageImpl();
			if (userID!=null&&userManager.isExistUid(userID.trim())) {     
				String userName = request.getParameter("userName");
				String company = request.getParameter("company");
				String password = request.getParameter("password");
				String userRole = request.getParameter("userRole");
				String contactEmail = request.getParameter("contactEmail");
				String userType = request.getParameter("userType");
				String emailNotifications = request
				.getParameter("emailNotifications");
				String passwordMD5 = MdPwd.getMD5(password);// MD5加密
				
				UserBean userBean = new UserBean();
				userBean.setCustomerID(company);
				userBean.setUserID(userID);
				userBean.setUserName(userName);
				userBean.setEmailAddress(contactEmail);
				userBean.setEmailNotifications(emailNotifications);
				userBean.setUserType(userType);
				
				UserPasswordBean userPasswordBean = new UserPasswordBean();
				userPasswordBean.setUserID(userID);
				userPasswordBean.setPassword(passwordMD5);
				userBean.setUserPasswordBean(userPasswordBean);
				
				UserRoleBean userRoleBean = new UserRoleBean();
				userRoleBean.setUserID(userID);
				userRoleBean.setRoleID(userRole);
				userBean.setUserRole(userRoleBean);
				
				int result = userManager.addUser(userBean);
				if (result > 0) {
					request.setAttribute("message", "添加用户 [ "+userID+" ]成功");
					
				} else {
					request.setAttribute("message", "添加用户 [ "+userID+" ]失败");
				}
				request.getRequestDispatcher(
				"/servlet/SrhUser").forward(
						request, response);
			} else {
				request.setAttribute("message", " [ "+userID+" ]用户名已被使用!");
			}
		}else if(key.equals("isExistCus")){
			String valiName=request.getParameter("cusId");
			valiName  = new String (valiName.getBytes("iso-8859-1"),"gbk");
			if(valiName!=null){
				this.isExsitName(valiName.trim(),response,"cus");
			}
		}else if(key.equals("isExistUser")){
			String valiName=request.getParameter("userId");
			valiName  = new String (valiName.getBytes("iso-8859-1"),"gbk");
			if(valiName!=null){
				this.isExsitName(valiName.trim(),response,"user");
			}
		}
		

	}

	
	/**
	 * 查询cusid是否存在
	 * @param name
	 * @param response
	 */
	private void isExsitName(String name, HttpServletResponse response,String type) {
		CustomerManager cusManage=new CustomerManagerImpl();
		PrintWriter out = null;
		try {
			out = response.getWriter();
			if(type.equals("cus")){
				if (cusManage.isExsitID(name)) {// 返回true表示用户名不存在,可用
					out.write("ok");
				} else {
					out.write("isExsit");
					
				}
			}else if(type.equals("user")) {
				if (new UserManageImpl().isExistUid(name)) {// 返回true表示用户名不存在,可用
					out.write("ok");
				} else {
					out.write("isExsit");
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	
}
